package top.zbsong.community.provider;

import com.alibaba.fastjson.JSON;
import okhttp3.*;
import org.springframework.stereotype.Component;
import top.zbsong.community.dto.AccessTokenDTO;
import top.zbsong.community.dto.OAuthUser;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Create By songzb on 2021/5/8
 *
 * @author songzb
 */
@Component
public class GiteeOAuthProvider implements OAuthProvider {
    @Override
    public String getAccessToken(AccessTokenDTO accessTokenDTO) {
        MediaType mediaType = MediaType.get("application/json; charset=utf-8");

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
                .readTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
                .writeTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
                .build();

        // RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(accessTokenDTO));
        //
        // Request request = new Request.Builder()
        //         .url("https://gitee.com/oauth/token")
        //         .post(body).build();

        RequestBody body = new FormBody.Builder().add("client_secret", accessTokenDTO.getClient_secret()).build();

        Request request = new Request.Builder()
                .url("https://gitee.com/oauth/token?grant_type=authorization_code&code=" + accessTokenDTO.getCode()
                        + "&client_id=" + accessTokenDTO.getClient_id()
                        + "&redirect_uri=" + accessTokenDTO.getRedirect_uri()
                ).post(body).build();
        try (Response response = client.newCall(request).execute()) {
            // System.out.println(response.body().string());
            //{"access_token":"f05165b22ad0063a9d216bc47fe64e6a",
            // "token_type":"bearer",
            // "expires_in":86400,
            // "refresh_token":"4a4922a44287ceaba43028904d5b1e377675842ed56754c1362db6766665da8c",
            // "scope":"user_info",
            // "created_at":1620472681}
            String token = (String) JSON.parseObject(response.body().string()).get("access_token");
            return token;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public OAuthUser getUser(String accessToken) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(60 * 1000, TimeUnit.MILLISECONDS)
                .readTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
                .writeTimeout(5 * 60 * 1000, TimeUnit.MILLISECONDS)
                .build();

        Request request = new Request.Builder()
                .url("https://gitee.com/api/v5/user?access_token=" + accessToken)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            OAuthUser OAuthUser = JSON.parseObject(string, OAuthUser.class);
            return OAuthUser;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
